import GeoJsonLayer from '../../src/layer/vector/GeoJsonLayer';
import VectorLayer from '../../src/layer/vector/VectorLayer';
import SourceType from '../../src/data/type/SourceType';
import GeoJsonSource from '../../src/data/source/vector/GeoJsonSource';
import WebFeatureSource from '../../src/data/source/web/WebFeatureSource';
import { SourceEventType } from '../../src/event/EventType';

import { registerLayerEvent } from '../event/layerEvent';
import CsvSource from '../../src/data/source/vector/CsvSource';

/**
 * 添加矢量图层
 * @param {} earth 
 */
// export default function add_vector_layer(earth){

//     //add_geojson_layer(earth);

//     /** 
//      * 通过Source添加Layer
//      */
//     //add_source_geojson_layer(earth);

//     //添加
//     //add_geojson_layer_deprecated(earth);
    
//     //添加ogc wfs图层
//     //add_ogc_wfs_layer(earth);

//     /**
//      * 添加CSV图层
//      * @Date 2023年3月6日14:17:27
//      */
//     add_csv_layer(earth);
// }


function add_geojson_layer(earth){

    //1. 创建Source
    let source = new GeoJsonSource("geojson", {
        //url : "/resource/json/beijing.json"
        //url : "/resource/json/cities.json"
        url : "/resource/json/csv_cities.json"
    });

    //2. 创建Layer，将Source作为Layer属性
    let layer = new GeoJsonLayer("geojson", {
        source : source
    });

    registerLayerEvent(layer);

    earth.addLayer(layer);    

    //earth.flyTo(layer);
    earth.zoomTo(layer);
}

function add_source_geojson_layer(earth){
    
    let sourceName = "geojson";

    //1. 创建Source    
    earth.addSource(
        new GeoJsonSource(sourceName, {
            //url : "/resource/json/beijing.json"
            url : "/resource/json/cities.json"
        })
    );
    let source = earth.getSource(sourceName);

    //2. 创建Layer，将Source作为Layer属性
    let layer = new GeoJsonLayer("geojson", {
        source : source
    });

    registerLayerEvent(layer);

    earth.addLayer(layer);    

    //earth.flyTo(layer);
    earth.zoomTo(layer);
}

function add_csv_layer(earth){
    let sourceName = "csv";

    //1. 创建Source    
    earth.addSource(
        new CsvSource(sourceName, {
            url : "/resource/csv/cities.csv",
            header : true,
            fields :  {
                x : "X",
                y : "Y"
            }
        })
    );
    let source = earth.getSource(sourceName);

    //2. 创建Layer，将Source作为Layer属性
    let layer = new VectorLayer(sourceName, {
        source : source
    });

    registerLayerEvent(layer);

    earth.addLayer(layer);    

    //earth.flyTo(layer);
    earth.zoomTo(layer);
}


/**
 * 添加ogc wfs的要素图层
 * @param {} earth 
 */
function add_ogc_wfs_layer(earth){

    let datasetName = "radi:cities";
    let wfs_url = "/geoserver/ows";

    //创建数据source
    let source = _create_wfs_source(datasetName, wfs_url);

    //创建图层layer
    let layer = new VectorLayer("geojson", {
        source : source
    });

    registerLayerEvent(layer);

    earth.addLayer(layer);    

    //earth.flyTo(layer);
    earth.zoomTo(layer);
}

function _create_wfs_source(datasetName, wfs_url){
    let filter = undefined;
    let source = new WebFeatureSource("xxx", {
        url : wfs_url,
        version : "1.1.0",
        typeName : "radi:china",
        fields : ["NAME", "CAPITAL"],
        filter : filter
    })

    /***
     * 触发数据获取成功的FETCHED事件，做事件的后续处理
     */
    // source.on(SourceEventType.FETCHED, function(evt){
    //     let json = evt.data;
    //     alert(json.type);
    //     alert("[Feature Count]:  " + json.features.length);
    // })

    /***
     * 触发数据获取失败的FETCH_ERROR事件，做事件的后续处理
     */
    // source.on(SourceEventType.FETCH_ERROR, function(evt){
    //     alert(evt.message);
    // });

    // let typeName = datasetName;
    // let query = create_query();
    //source.query(typeName, query);

    return source
}

/**
 * 2023年2月22日08:47:42
 */
function create_query(){
    let query = new Query();
    query.addFields(["NAME", "CAPITAL"]);

    return query;
}


/**
 * @deprecated
 * @param {} earth 
 */
function add_geojson_layer_deprecated(earth){
    //1. 添加GeoJson图层
    let layer = new GeoJsonLayer(
        "geojson",
        "/resource/json/beijing.json",
        {}
    );
    registerLayerEvent(layer);

    earth.addLayer(layer);    

    //earth.flyTo(layer);
    earth.zoomTo(layer);
}

